package wifiunion.com.notice.widget.filterCommunityUuiddialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.TextView;



import java.util.ArrayList;
import java.util.HashMap;

import wifiunion.com.notice.BasePresenter;
import wifiunion.com.notice.CommonUtils;
import wifiunion.com.notice.Constants;
import wifiunion.com.notice.R;
import wifiunion.com.notice.RecyclerItemClick;
import wifiunion.com.notice.entity.User;
import wifiunion.com.notice.utils.SharedPreferencesUtils;

/**
 * Created by Administrator on 2016/12/5.
 * 筛选场景
 */

public class FilterCommunityUuidSthDialog extends Dialog implements RecyclerItemClick,FilterUuidContact.View {
    private Context context;
    private TextView titleName;

    private FilterCommunityUuidSthAdapter mAdapter;

    private ArrayList<FilterUuidEntity> mList = new ArrayList<FilterUuidEntity>();

    private TextView cancelTxt;
    private TextView confirmTxt;


    private RecyclerView mRecyclerView;


    private FilterUuidDialogCallBack mCallback;

    private boolean isSingleSelect = false;
    /**
     * 是否有全部这个选项 涉及到选择全部选项的时候，其他子项被取消选择
     */
    private boolean hasAllItem = true;

    private FilterUuidPresenter mFilterUuidPresenter;

    //选中的位置
    private int checkedPostion;

    private HashMap<String, Object> mParams = new HashMap<>();
    private String mAuthtoken;
    private String mUuid;
    private FilterUuidEntity chooseFilterUuidEntity;

    public FilterCommunityUuidSthDialog(Context context, String uuid, FilterUuidDialogCallBack callback, FilterUuidEntity mChooseFilterUuidEntity) {
        super(context, R.style.alert_dialog);
        this.context = context;
        this.mUuid = uuid;
        this.mCallback = callback;
        this.chooseFilterUuidEntity = mChooseFilterUuidEntity;

        LayoutInflater li = LayoutInflater.from(context);
        ViewGroup contentView = (ViewGroup) li.inflate(
                R.layout.dialog_deviceselect, null);
        contentView.bringToFront();


        cancelTxt = (TextView) contentView.findViewById(R.id.cancel_txt);
        confirmTxt = (TextView) contentView.findViewById(R.id.confirm_txt);
        titleName = (TextView) contentView.findViewById(R.id.tv_title_name);
        cancelTxt = (TextView) contentView.findViewById(R.id.cancel_txt);

        mRecyclerView = (RecyclerView) contentView.findViewById(R.id.deviceselect_recyclerview);


        mAdapter = new FilterCommunityUuidSthAdapter(getContext(), mList, this);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(context));

        mRecyclerView.setAdapter(mAdapter);


        mAdapter.setOnclickListener(this);

        //这里的token从起初登录时保存的token中获取
        mAuthtoken = SharedPreferencesUtils.getSharedPreferences(Constants.CONFIG_PLACE_AUTHTOKEN, context);

        setCancelListener();
        setComfirmListener();

        setCanceledOnTouchOutside(true);

        setContentView(contentView);

        Constants.refreshIP();

        setData();


    }

    /**
     * 获取全部路径数据
     */
    private void setData() {
        mFilterUuidPresenter = new FilterUuidPresenter(this, 0);
        mParams.clear();
        mParams.put("Authorization", mAuthtoken);
        mParams.put("menuUuid", mUuid);
        mFilterUuidPresenter.getTreeFilter(mParams);
    }




    public void setIsSingleSelect(boolean flag) {
        isSingleSelect = flag;
    }

    public void setHasAllItem(boolean flag) {
        hasAllItem = flag;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.gravity = Gravity.CENTER;
        lp.width = getContext().getResources().getDimensionPixelSize(
                R.dimen.dp_480);
        lp.height = (int) (CommonUtils.getScreenHeight(getContext()) * 0.9f);
        onWindowAttributesChanged(lp);
    }

    public void setTitleMsg(String msg) {
        if (titleName != null) {
            titleName.setText(msg);
        }
    }

    public void setCancleText(String text) {
        if (text != null) {
            cancelTxt.setText(text);
        }
    }


    public void setComfirmListener() {
        if (confirmTxt != null) {
            confirmTxt.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    mCallback.getData(mList.get(checkedPostion));
                    dismiss();
                }

            });
        }
    }

    public void setCancelListener() {
        if (cancelTxt != null) {
            cancelTxt.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dismiss();
                }
            });
        }
    }


    @Override
    public void onItemClick(View view, int postion) {
//        if (isSingleSelect) {
            for (int i = 0; i < mList.size(); i++) {
                if (postion == i) {
                    checkedPostion = postion;
                    mList.get(i).setSelected(true);
                } else {
                    mList.get(i).setSelected(false);
                }
            }
//        } else {
//
//            if (mList.get(postion).isSelected()) {
//                mList.get(postion).setSelected(false);
//            } else {
//                // 不限 项被选中 其他项 取消选中
//                if (postion == 0) {
//                    if (hasAllItem) {
//                        for (int i = 0; i < mList.size(); i++) {
//                            if (postion != i) {
//                                mList.get(i).setSelected(false);
//                            }
//                        }
//                    }
//                } else {
//                    if (hasAllItem) {
//                        //非 不限 项被选中  不限项取消选中
//                        if (mList.get(0).isSelected()) {
//                            mList.get(0).setSelected(false);
//                        }
//                    }
//                }
//                mList.get(postion).setSelected(true);
//            }
//        }
        mAdapter.notifyDataSetChanged();
    }

    @Override
    public void onProgress() {

    }

    @Override
    public void onError(Object obj) {

    }

    @Override
    public void onSuccess(Object obj) {

        if (obj != null) {

            FilterUuidEntity zysj =  new FilterUuidEntity();
            User user = SharedPreferencesUtils.getPlaceUserSharedPreferences(getContext());
            zysj.setCommunityUuid(user.getCommunityUuid());
            zysj.setUuid("-1");
            zysj.setName("自有数据");
            mList.add(zysj);
            ArrayList<FilterUuidEntity> list = (ArrayList<FilterUuidEntity>) obj;
            if(null != list && list.size() > 0)
            {
                mList.addAll(list);
            }
            if(null == chooseFilterUuidEntity)
            {
                mList.get(0).setSelected(true);
            }else
            {
                for(int i=0;i<mList.size();i++)
                {
                    if(chooseFilterUuidEntity.getCommunityUuid().equals(mList.get(i).getCommunityUuid()))
                    {
                        checkedPostion = i;
                        mList.get(i).setSelected(true);
                        break;
                    }
                }

            }

            mAdapter.notifyDataSetChanged();
        }
    }






    @Override
    public void getCurrentTreeSuccess(Object obj) {

    }

    @Override
    public void getCurrentTreeFailed(Object obj) {

    }

    @Override
    public void setPresenter(BasePresenter tp) {

    }
}
